package com.example.user.service;

import com.example.common.Ov.BankOv;
import com.example.common.Ov.UserOv;
import com.example.common.entity.Rsp;
import com.example.common.pojo.BankCard;
import com.example.common.pojo.User;
import com.example.user.mapper.BankCardMapper;
import com.example.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class BankServiceImpl implements BankService {

    @Autowired
    BankCardMapper bankCardMapper;

    @Autowired
    RedisServiceImpl redisService;

    @Autowired
    UserMapper userMapper;

    //注册存管
    @Override
    public Rsp registerBank(BankOv bankOv, UserOv userOv) {
        String o = (String)redisService.get(bankOv.getMobile());
        if(!o.equals(bankOv.getMsgCode())){
            //不相等，验证码输入错误
            return new Rsp(0,"验证码错误");
        }
        //验证码正确，去掉redis中的验证码
        redisService.remove(bankOv.getMobile());
        //添加银行卡
        BankCard bankCard = new BankCard();
        bankCard.setUserId(userOv.getId());
        bankCard.setRealName(bankOv.getFullname());
        bankCard.setCardNum(bankOv.getCardNumber());
        bankCard.setProvince("湖北");
        bankCard.setCity("武汉");
        bankCard.setBank("工商银行");
        bankCard.setOpeningBank("工商银行");
        int result = bankCardMapper.insertSelective(bankCard);
        //添加支付密码
        User user = new User();
        user.setId(userOv.getId());
        user.setPayPassword(bankOv.getPayPwd());
        int i = userMapper.updateByPrimaryKeySelective(user);
        return result+i>1?new Rsp(0,"开卡成功"):new Rsp(0,"开卡失败");
    }

    //获取银行卡信息--
    @Override
    public Rsp getCardInfo(UserOv userOv) {
        BankCard bankCard = new BankCard();
        bankCard.setUserId(userOv.getId());
        List<BankCard> bankCards = bankCardMapper.getByCondition(bankCard);
//        if(StringUtils.isEmpty(bankCards.get(0))){
//            //没有绑定过银行卡
//            return new Rsp(0,"暂无银行卡信息");
//        }
//        //
        return new Rsp(0,bankCards);
    }
}
